package casa.depesos.app.com.activity;

import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import java.util.HashMap;

import casa.depesos.app.com.App;
import casa.depesos.app.com.R;
import casa.depesos.app.com.adapter.OrderDetailAdapter;
import casa.depesos.app.com.databinding.ActivityOrderDetailBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.OrderDetailBean;
import casa.depesos.app.com.http.data.Order;
import casa.depesos.app.com.http.data.OrderDetail;
import casa.depesos.app.com.http.req.OrderDetailReq;
import casa.depesos.app.com.utils.Utils;

public class OrderDetailActivity extends BaseActivity {

    ActivityOrderDetailBinding binding;
    Order order;
    OrderDetail detail;
    HashMap<String, String> map = new HashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] title = App.getApp().getResources().getStringArray(R.array.orders);
        String[] status = App.getApp().getResources().getStringArray(R.array.orders_status);
        for (int i = 0; i < status.length; i++) {
            map.put(status[i], title[i]);
        }

        binding = ActivityOrderDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.top.back.setOnClickListener(v -> finish());
        binding.top.title.setText("Gestión de pedidos");
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        order = getIntent().getParcelableExtra("order");

        getData();
    }

    private void getData() {
        showLoading();
        new OrderDetailReq(order.no).req(new ReqCallback<OrderDetailBean>() {
            @Override
            public void callback(boolean success, OrderDetailBean bean) {
                hideLoading();
                if (success && bean.isValid()) {
                    detail = bean.order;
                    show();
                }
            }
        });
    }

    private void show() {
        binding.amount2.setText(Utils.formatNoFloat(detail.amount, true));
        binding.amount.setText(Utils.formatNoFloat(detail.amount));
        if (map.containsKey(detail.status)) {
            binding.status.setText(map.get(detail.status));
        } else {
            binding.status.setText("");
        }
        switch (detail.status) {
            case "10":
                binding.status.setBackgroundResource(R.drawable.order_10);
                break;
            case "70":
                binding.status.setBackgroundResource(R.drawable.order_red);
                break;
            case "20":
                binding.status.setBackgroundResource(R.drawable.order_20);
                break;
            case "30":
                binding.status.setBackgroundResource(R.drawable.order_30);
                break;
            case "40":
                if (detail.isOverdue) {
                    binding.status.setBackgroundResource(R.drawable.order_10);
                } else {
                    binding.status.setBackgroundResource(R.drawable.order_gray);
                }
                break;
            case "60":
                binding.status.setBackgroundResource(R.drawable.order_60);
                break;
        }
        new OrderDetailAdapter(detail, binding.list).fill();

        binding.name.setText(detail.name);
        binding.limit.setText((Float.valueOf(detail.day).intValue() + 1) + "días");
        binding.account.setText(detail.card);
        binding.fee.setText(getValue(detail.total));
        binding.interest.setText(getValue(detail.interest));
        binding.late.setText(getValue(detail.forfeit));
        binding.iva.setText(getValue(detail.vat));
        binding.left.setText(getValue(detail.remain));
        binding.pay.setText(getValue(detail.indeed));
    }

    private String getValue(String str) {
        if (TextUtils.isEmpty(str)) {
            return "0";
        }
        return str;
    }

    @Override
    protected boolean needPadding() {
        return false;
    }
}
